import { ui } from "../../ui/layaMaxUI";
import GameEvent from "../../core_wq/game/GameEvent";
import Game from "../../core_wq/game/Game";
import GlobalData from "../../core_wq/db/GlobalData";
import Hero from "../hall/hero/Hero";
import HeroVO from "../../core_wq/db/vo/HeroVO";
import StageVO from "../../core_wq/db/vo/StageVO";
import { GameStageType } from "../../core_wq/game/GameStageType";



export default class LevelProgressView extends ui.moduleView.main.LevelProgressViewUI {

    _isInit = false;



    onEnable() {
        if (this.bar1 && this._isInit !== true) {
            this._isInit = true;
            this.scale(0, 0);
            Laya.stage.on(GameEvent.START_GAME, this, this.hide);
            Laya.stage.on(GameEvent.SHOW_MAIN, this, this.show);
            Laya.stage.on(GameEvent.LEVEL_UPDATE, this, this.update);
        }
    }


    hide() {
        Laya.Tween.clearAll(this);
        Laya.Tween.to(this, { scaleX: 0, scaleY: 0 }, 300, Laya.Ease.sineOut);


    }

    show() {
        Laya.Tween.clearAll(this);
        Laya.Tween.to(this, { scaleX: 1, scaleY: 1 }, 300, Laya.Ease.sineOut);


    }

    update() {
        let firstLevel = 1;
        let endLevel = -1;
        // console.log("Game.gameStage.stageData.curLevel", Game.gameStage.stageData.curLevel)

        if (Game.gameStage.stageVO.stageType===GameStageType.BOSS) {
            endLevel = Game.gameStage.stageData.curLevel;
        }

        GlobalData.getDataByCondition(GlobalData.StageVO, (data: StageVO) => {

            if (data.stageType===GameStageType.BOSS) {
                if ((data.id < Game.gameStage.stageData.curLevel && (Game.gameStage.stageData.curLevel - data.id) <= 10)) {
                    // console.log("data id",data)
                    firstLevel = data.id + 1;

                    return true;
                }
                else if ((data.id > Game.gameStage.stageData.curLevel) && ((data.id - Game.gameStage.stageData.curLevel) <= 10)) {
                    if (endLevel === -1) {
                        endLevel = data.id;
                        return true;
                    }
                }
            }
            return false;

        });
        let isTen = (endLevel - firstLevel > 6);
        // console.log(firstLevel, endLevel)
        for (let i = firstLevel; i <= endLevel; i++) {
            if (isTen) {
                this.setProgressState(i, i % 10 - 1);
            }
            else {
                let index = i % 5;
                if (index === 1) {
                    this.setProgressState(i, 0);
                    (this.bg.getChildAt(1) as Laya.Image).alpha = 0;
                }
                else if (index === 0) {
                    this.setProgressState(i, 9);
                    (this.bg.getChildAt(8) as Laya.Image).alpha = 0;

                }
                else {
                    this.setProgressState(i, index + index - 2);
                    (this.bg.getChildAt(index + index - 1) as Laya.Image).alpha = 0;

                }
            }


        }


    }
    private setProgressState(levelIndex, childIndex) {
        if (childIndex === -1) {
            childIndex = 9;
        }
        // console.log("levelIndex,childindex", levelIndex, childIndex)
        let child: Laya.Image = (this.bg.getChildAt(childIndex) as Laya.Image)
        // console.log(child);
        if (levelIndex === Game.gameStage.stageData.curLevel) {
            child.skin = "images/main/level_progress_state3.png";
        }
        else if (levelIndex < Game.gameStage.stageData.curLevel) {
            child.skin = "images/main/level_progress_state2.png";
        }
        else if (levelIndex > Game.gameStage.stageData.curLevel) {
            child.skin = "images/main/level_progress_state1.png";
        }
        child.alpha = 1;
    }



}